//
//  ViewController.m
//  Picture Carousel Figure
//
//  Created by apple on 15/11/3.
//  Copyright © 2015年 renwei. All rights reserved.
//

#import "ViewController.h"
#define imageCount 5

@interface ViewController () <UIScrollViewDelegate>

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;
@property (weak, nonatomic) IBOutlet UIPageControl *pageControl;
@property (nonatomic, strong) NSTimer *timer;
@end

@implementation ViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    CGFloat imageW = self.scrollView.frame.size.width;
    CGFloat imageH = self.scrollView.frame.size.height;
    
    for (int i = 1; i < imageCount + 1; i++) {
        
        CGFloat imageX = (i - 1) * imageW;
        CGFloat imageY = 0;
        
        UIImageView *imageView = [[UIImageView alloc]initWithFrame:CGRectMake(imageX, imageY, imageW, imageH)];
        
        imageView.image = [UIImage imageNamed:[NSString stringWithFormat:@"img_%02d", i]];
        
        [self.scrollView addSubview:imageView];
    
    }
    
    self.scrollView.contentSize = CGSizeMake(imageCount * imageW, 0);
    
    // 拖动分页
    self.scrollView.pagingEnabled = YES;
    
    // 设置代理
    self.scrollView.delegate = self;
    
    // 设置总页
    self.pageControl.numberOfPages = imageCount;
    
    // 创建定时器
    [self addScrollTimer];
}

- (void)addScrollTimer
{
    
    //    创建定时器（两种方式：timerWithTimeInterval）
    //    self.timer = [NSTimer scheduledTimerWithTimeInterval:1.f target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
    
    //
    self.timer = [NSTimer timerWithTimeInterval:1.0 target:self selector:@selector(nextPage) userInfo:nil repeats:YES];
    [[NSRunLoop mainRunLoop] addTimer:self.timer forMode:NSRunLoopCommonModes];
}

- (void)removeScrollTimer
{
    [self.timer invalidate];
    self.timer = nil;
    
}

// 下一页
- (void)nextPage {
    
    long currentPage = self.pageControl.currentPage;
    currentPage ++;

    if (currentPage == 5) {
        currentPage = 0;
    }
    
    CGFloat width = self.scrollView.frame.size.width;
    CGPoint offset = CGPointMake(currentPage * width, 0);
    //self.scrollView.contentOffset = offset;
    // 第一种动画效果
    //[self.scrollView setContentOffset:offset animated:YES];
    // 第二种动画效果
    [UIView animateWithDuration:2.0 animations:^{
        self.scrollView.contentOffset = offset;
    }];
}

#pragma mark - UIScrollViewDelegate
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
    CGPoint offset = self.scrollView.contentOffset;
    CGFloat offsetX = offset.x;
    CGFloat width = self.scrollView.frame.size.width;
    int pageNum = (offsetX + 0.5 * width) / width;
    
    self.pageControl.currentPage = pageNum;
}

- (void)scrollViewWillBeginDragging:(UIScrollView *)scrollView
{
    // NSLog(@"scrollViewWillBeginDragging");
    [self removeScrollTimer];
}

- (void)scrollViewDidEndDragging:(UIScrollView *)scrollView willDecelerate:(BOOL)decelerate
{
    // NSLog(@"scrollViewDidEndDraggingr");
    [self addScrollTimer];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end
